home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9569 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.5 KB  |  143 lines

  1. Path: newsfeed.internetmci.com!panix!not-for-mail
  2. From: acinader@panix.com (Arthur Cinader Jr)
  3. Newsgroups: comp.lang.c++,gnu.g++.help
  4. Subject: DONT UNDERSTAND - array of char error
  5. Date: 2 Mar 1996 15:44:46 -0500
  6. Organization: Panix
  7. Message-ID: <4habvu$99j@panix.com>
  8. NNTP-Posting-Host: panix.com
  9.  
  10. I am teaching myself c++ from the Deitel & Deitel text:
  11. "Learning to Program C++"  I am workin on one of the
  12. excercises.
  13.  
  14. I have built a class called Param for passing labels and
  15. parameters around.  I want to store the labels and parameters
  16. in an array of strings.  To this end, I had the following
  17. declaration in my header file:
  18.  
  19.     char *labels[];
  20.     char *params[];
  21.  
  22. the compiler, g++, gave me the following error:
  23.  
  24.     param.h:21: field `labels' has incomplete type
  25.  
  26. I don't understand why this doesn't work.  
  27.  
  28. In any event, I changed the declaration to:
  29.  
  30.     char **labels;
  31.     char **params;
  32.  
  33. Which the compiler liked.  Now, I am unable to new the arrays.
  34. In the definition (.C) file, I use the comand:
  35.  
  36.     labels = new char[maxargs][fieldlength]; // make label array
  37.     params = new char[maxargs][fieldlength]; // make params array
  38.  
  39. The compiler complains with the following:
  40.  
  41.     param.C: In method `Param::Param(char *)':
  42.     param.C:11: assignment to `char **' from `char (*)[1]'
  43.     param.C:16: assignment to `char **' from `char (*)[1]'
  44.  
  45. I do not understand why?
  46.  
  47. I have included the source for the class below.  Any help 
  48. is appreciated. 
  49.  
  50. Arthur
  51. <mailto:acinader@panix.com>
  52.  
  53. P.S. do the deletes in my destructor look kosher?
  54.  
  55.  
  56. ******begin source****
  57.  
  58. // Parameter class for passing labels and parameters
  59.  
  60. #ifndef PARAM_H
  61. #define PARAM_H
  62.  
  63. class Param {
  64. public:
  65.         Param(char *);
  66.         void addlabel(char *);
  67.         void nextlabel();
  68.         const char *getlabel() const;
  69.         int getlabelcnt() const;
  70.         void addparam(char *);
  71.         void nextparam();
  72.         const char *getparam() const;
  73.         int getparamcnt() const;
  74.         ~Param();
  75. private:
  76.         const int maxargs;
  77.         const int fieldlength;
  78.         char **labels;
  79.         int labelcnt;
  80.         char *labelitr; // label iterator
  81.         char **params;
  82.         int paramcnt;
  83.         char *paramitr; // parameter iterator
  84.  
  85. };
  86.  
  87. #endif
  88.  
  89.  
  90. // Param class definition
  91.  
  92. #include<iostream.h>
  93. #include<assert.h>
  94. #include "param.h"
  95.  
  96. // Constructor
  97. Param::Param(char *shape)
  98.         :maxargs(10),fieldlength(20)
  99. {
  100.         labelcnt = 0;
  101.         labels = new char[maxargs][fieldlength]; // make label array
  102.         assert(labels != 0); // make sure memory allocated
  103.         labelitr = labels[labelcnt]; // set lablitr to first label
  104.         addlabel(shape); // set first label to shape name
  105.  
  106.         paramcnt = 0;
  107.         params = new char[maxargs][fieldlength]; // make params array
  108.         assert(params != 0); // make sure memorey allocated
  109.         paramitr = params[paramcnt];
  110. }
  111.  
  112. // add label
  113. void Param::addlabel(char *label) { *labels[labelcnt++] = *label; }
  114.  
  115. // label iterator
  116. void Param::nextlabel() { ++labelitr; }
  117.  
  118. // get label --  copy result to string
  119. const char *Param::getlabel() const { return labelitr; }
  120.  
  121. // get label count
  122. int Param::getlabelcnt() const { return labelcnt; }
  123.  
  124. // add parameter
  125. void Param::addparam(char *param) { *params[paramcnt++] = *param; }
  126.  
  127. // parameter iterator
  128. void Param::nextparam() { ++paramitr; }
  129.  
  130. // get parameter -- copy result to string
  131. const char *Param::getparam() const { return paramitr; }
  132.  
  133. // get parameter count
  134. int Param::getparamcnt() const { return paramcnt; }
  135.  
  136. // destructor
  137. Param::~Param()
  138.  
  139. {
  140.         delete [] labels;
  141.         delete [] params;
  142. }
  143.